import 'dart:async';
import 'dart:math';

import 'package:aichat_flutter_wallet/module_common/utils/colors.dart';
import 'package:flutter/material.dart';

class CountdownProgressBar extends StatefulWidget {
  final double strokeWidth;
  final Color backgroundColor;
  final Color progressColor;
  final int totalTime;
  final VoidCallback onCountdownFinish;

  CountdownProgressBar({
    this.strokeWidth = 2.0,
    this.backgroundColor = AppColors.c_F3F2F7,
    this.progressColor = AppColors.c_8667FE,
    required this.totalTime,
    required this.onCountdownFinish,
  });

  @override
  _CountdownProgressBarState createState() => _CountdownProgressBarState();
}

class _CountdownProgressBarState extends State<CountdownProgressBar> {
  double _progressValue = 0.0;
  Timer? _timer;
  int _elapsedSeconds = 0;

  @override
  void initState() {
    super.initState();
    _startTimer();
  }

  @override
  void dispose() {
    _timer?.cancel();
    super.dispose();
  }

  void _startTimer() {
    const oneSec = Duration(seconds: 1);
    _timer = Timer.periodic(oneSec, (Timer timer) {
      setState(() {
        _elapsedSeconds++;
        _progressValue = _elapsedSeconds / widget.totalTime;
        if (_elapsedSeconds == widget.totalTime) {
          timer.cancel();
          widget.onCountdownFinish(); // 在倒计时结束后执行功能
          _restartTimer();
        }
      });
    });
  }

  void _restartTimer() {
    setState(() {
      _elapsedSeconds = 0;
      _progressValue = 0.0;
    });
    _startTimer();
  }

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: _CountdownProgressBarPainter(
        strokeWidth: widget.strokeWidth,
        backgroundColor: widget.backgroundColor,
        progressColor: widget.progressColor,
        progressValue: _progressValue,
      ),
      child: SizedBox(
        width: 100.0,
        height: 100.0,
      ),
    );
  }
}

class _CountdownProgressBarPainter extends CustomPainter {
  final double strokeWidth;
  final Color backgroundColor;
  final Color progressColor;
  final double progressValue;

  _CountdownProgressBarPainter({
    required this.strokeWidth,
    required this.backgroundColor,
    required this.progressColor,
    required this.progressValue,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final center = Offset(size.width / 2, size.height / 2);
    final radius = min(size.width / 2, size.height / 2) - strokeWidth / 2;

    final backgroundPaint = Paint()
      ..color = backgroundColor
      ..style = PaintingStyle.stroke
      ..strokeWidth = strokeWidth;

    final progressPaint = Paint()
      ..color = progressColor
      ..style = PaintingStyle.stroke
      ..strokeWidth = strokeWidth
      ..strokeCap = StrokeCap.round;

    canvas.drawCircle(center, radius, backgroundPaint);

    final sweepAngle = 2 * pi * progressValue;
    canvas.drawArc(
      Rect.fromCircle(center: center, radius: radius),
      -pi / 2,
      sweepAngle,
      false,
      progressPaint,
    );
  }

  @override
  bool shouldRepaint(_CountdownProgressBarPainter oldDelegate) {
    return progressValue != oldDelegate.progressValue ||
        progressColor != oldDelegate.progressColor ||
        backgroundColor != oldDelegate.backgroundColor ||
        strokeWidth != oldDelegate.strokeWidth;
  }
}
